﻿using System;
using System.Web.Profile;
using System.Web.Security;
using System.Globalization;

namespace BlogEngine.Core.Azure.BlogProvider
{
    public class UserProfile : ProfileBase
    {
        public static UserProfile GetUserProfile(string username)
        {
            return Create(username) as UserProfile;
        }

        public static UserProfile GetUserProfile()
        {
            return Create(Membership.GetUser().UserName) as UserProfile;
        }

        public static explicit operator AuthorProfile(UserProfile p)
        {
            AuthorProfile profile = new AuthorProfile(p.UserName);
            profile.AboutMe = p.AboutMe;
            DateTime date; // TODO: This used to be a direct DateTime.Parse(); why does it no longer work?
            DateTime.TryParse(p.Birthday, CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.None, out date);
            profile.Birthday = date;
            profile.CityTown = p.CityTown;
            profile.Company = p.Company;
            profile.Country = p.Country;
            profile.DisplayName = p.DisplayName;
            profile.EmailAddress = p.EmailAddress;
            profile.FirstName = p.FirstName;
            profile.IsPrivate = p.IsPrivate;
            profile.LastName = p.LastName;
            profile.MiddleName = p.MiddleName;
            profile.PhoneFax = p.PhoneFax;
            profile.PhoneMain = p.PhoneMain;
            profile.PhoneMobile = p.PhoneMobile;
            profile.PhotoURL = p.PhotoURL;
            profile.RegionState = p.RegionState;
            return profile;
        }

        public static implicit operator UserProfile(AuthorProfile p)
        {
            UserProfile profile = UserProfile.GetUserProfile(p.UserName);
            profile.AboutMe = p.AboutMe;
            profile.Birthday = p.Birthday.ToString(CultureInfo.InvariantCulture.DateTimeFormat);
            profile.CityTown = p.CityTown;
            profile.Company = p.Company;
            profile.Country = p.Country;
            profile.DisplayName = p.DisplayName;
            profile.EmailAddress = p.EmailAddress;
            profile.FirstName = p.FirstName;
            profile.IsPrivate = p.IsPrivate;
            profile.LastName = p.LastName;
            profile.MiddleName = p.MiddleName;
            profile.PhoneFax = p.PhoneFax;
            profile.PhoneMain = p.PhoneMain;
            profile.PhoneMobile = p.PhoneMobile;
            profile.PhotoURL = p.PhotoURL;
            profile.RegionState = p.RegionState;
            return profile;
        }

        [SettingsAllowAnonymous(false)]
        public string DisplayName
        {
            get { return base["DisplayName"] as string; }
            set { base["DisplayName"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string FirstName
        {
            get { return base["FirstName"] as string; }
            set { base["FirstName"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string MiddleName
        {
            get { return base["MiddleName"] as string; }
            set { base["MiddleName"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string LastName
        {
            get { return base["LastName"] as string; }
            set { base["LastName"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string CityTown
        {
            get { return base["CityTown"] as string; }
            set { base["CityTown"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string RegionState
        {
            get { return base["RegionState"] as string; }
            set { base["RegionState"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string Country
        {
            get { return base["Country"] as string; }
            set { base["Country"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string Birthday
        {
            get { return base["Birthday"] as string; }
            set { base["Birthday"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string AboutMe
        {
            get { return base["AboutMe"] as string; }
            set { base["AboutMe"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string PhotoURL
        {
            get { return base["PhotoURL"] as string; }
            set { base["PhotoURL"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string Company
        {
            get { return base["Company"] as string; }
            set { base["Company"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string EmailAddress
        {
            get { return base["EmailAddress"] as string; }
            set { base["EmailAddress"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string PhoneMain
        {
            get { return base["PhoneMain"] as string; }
            set { base["PhoneMain"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string PhoneMobile
        {
            get { return base["PhoneMobile"] as string; }
            set { base["PhoneMobile"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public string PhoneFax
        {
            get { return base["PhoneFax"] as string; }
            set { base["PhoneFax"] = value; }
        }

        [SettingsAllowAnonymous(false)]
        public bool IsPrivate
        {
            get { return (bool)(base["IsPrivate"]); }
            set { base["IsPrivate"] = value; }
        }
    }
}